<?php
$this->breadcrumbs = array(
	'材料单据列表' => array(
		'index'
	),
	$model->project->name . ' (' . $model->receiptDate . ')' => array(
		'view',
		'id' => $model->id
	),
	'更新内容',
);

$this->userActions = array(
	array(
		'label' => '新建材料票据',
		'url' => array(
			'create'
		),
		'iconClass' => 'icon-basket-add'
	),
	array(
		'label' => '查看材料票据',
		'url' => array(
			'view',
			'id' => $model->id
		),
		'iconClass' => 'icon-basket'
	),
	array(
		'label' => '管理材料票据',
		'url' => array(
			'admin'
		),
		'iconClass' => 'icon-basket'
	),
);
?>

<h1>更新材料票据内容</h1>
<div class="">
<div class="span-2"><?php echo CHtml::link(CHtml::encode($model->receiptDate),
						array(
							'view',
							'id' => $model->id
						), array(
							'target' => '_blank'
						));
					?></div>

<div class="span-3"><?php echo CHtml::encode($model->project->name); ?></div>
<div class="span-3">
	<?php echo CHtml::encode($model->getAttributeLabel('handler')); ?>:
	<?php echo CHtml::encode($model->handler); ?>
</div>
<div class="span-3">
	<?php echo CHtml::encode($model->getAttributeLabel('type')); ?>:
	<?php echo CHtml::encode($model->displayType); ?>
</div>
<div class="span-4">
	<?php echo CHtml::encode($model->getAttributeLabel('token')); ?>:
	<?php echo CHtml::encode($model->token); ?>
</div>
<div class="span-4 last">
</div>
<div class="clear"></div>

</div>
<div>
<?php
$items = $model->items;
?>
<div class="update-items-grid">
<div class="form">
<?php echo CHtml::beginForm(array(
	'updateItems',
	'id' => $model->id
));
?>
<?php
$items = array_slice($model->items, 0);
$items[] = $newItem;

$this
	->widget('zii.widgets.grid.CGridView',
		array(
			'id' => 'material-receipt-items-grid',
			'dataProvider' => new CArrayDataProvider($items,
				array(
					'pagination' => false
				)),
			'enablePagination' => false,
			'columns' => array(
				array(
					'header' => '',
					'type' => 'raw',
					'value' => '$row+1'
				),
				array(
					'header' => '物品',
					'type' => 'raw',
					'value' => 'Yii::app()->getController()
					->widget("widgets.material.JMaterialModelSelector",
					array(
						"model" => $data,
						"attribute" => "[$row]materialModelId",
						"unitInputId"=>CHtml::activeId($data, "[$row]unit"),
						"unitTextId"=>"item-standard-unit-$row",
						"onSelectCallback"=>"function(model){
	JX.Material.updateMaterialRequisitionHistory(model.id,\\"item-requisition-history-$row\\",\\"item-requisition-last-$row\\",'
						. '{date:\\"' . $model->receiptDate . '\\",projectId:' . $model->projectId
						. '});
	JX.Material.updateMaterialWarehouseInboundHistory(model.id,\\"item-warehouse-inbound-history-$row\\",\\"item-warehouse-inbound-last-$row\\",'
						. '{date:\\"' . $model->receiptDate . '\\",projectId:' . $model->projectId
						. '});
	JX.Material.updateMaterialReceiptHistory(model.id,\\"item-receipt-history-$row\\",\\"item-receipt-last-$row\\",'
						. '{id:' . $model->id . '});
}",
					), true);',
				//'footer' => $model->isBalanced() ? "" : "X"
				),
				array(
					'header' => '品牌',
					'type' => 'raw',
					'value' => 'CHtml::activeTextField($data, "[$row]brand",array("size"=>"3"));',
				),
				array(
					'header' => '数量',
					'type' => 'raw',
					'value' => 'CHtml::activeTextField($data, "[$row]amount",array("size"=>"3"));',
				),
				array(
					'header' => '单位',
					'type' => 'raw',
					'value' => 'CHtml::activeTextField($data, "[$row]unit",array("size"=>"3"));',
				),
				array(
					'header' => '单价',
					'type' => 'raw',
					'value' => 'CHtml::activeTextField($data, "[$row]unitPrice",array("size"=>"3"));',
				),
				array(
					'header' => '折合',
					'type' => 'raw',
					'value' => 'CHtml::activeTextField($data, "[$row]amountStandardUnit",array("size"=>"3"));',
				),
				array(
					'header' => '标单',
					'headerHtmlOptions' => array(
						'style' => 'width: 32px;'
					),
					'type' => 'raw',
					'value' => '"<span id=\\"item-standard-unit-$row\\">".
					($data->materialModel?$data->materialModel->unit:"")."</span>"',
				),
				array(
					'header' => '标价',
					'type' => 'raw',
					'value' => 'CHtml::activeTextField($data, "[$row]standardUnitPrice",array("size"=>"3"));',
				),
				array(
					'header' => '总价',
					'type' => 'raw',
					'value' => 'CHtml::activeTextField($data, "[$row]totalPrice",array("size"=>"3"));',
					'footer' => Yii::app()->format->formatCurrency($model->itemTotal)
				),
				array(
					'header' => '备注',
					'type' => 'raw',
					'value' => 'CHtml::activeTextField($data, "[$row]note",array("size"=>"4"));',
				),
				array(
					'header' => '已申购',
					'headerHtmlOptions' => array(
						'style' => 'width: 60px;'
					),
					'type' => 'raw',
					'value' => '"<span id=\\"item-requisition-history-$row\\">".
					($data->materialModelId?MaterialManager::getMaterialRequisitionHistory($data->materialModelId,
			' . $model->projectId . ',"' . $model->receiptDate . '"):"")."</span>"',
				),
				array(
					'header' => '上次申购',
					'headerHtmlOptions' => array(
						'style' => 'width: 88px;'
					),
					'type' => 'raw',
					'value' => '"<span id=\\"item-requisition-last-$row\\">".
					($data->materialModelId?MaterialManager::getMaterialRequisitionLast($data->materialModelId,
			' . $model->projectId . ',"' . $model->receiptDate . '"):"")."</span>"',
				),
				array(
					'header' => '已入库',
					'headerHtmlOptions' => array(
						'style' => 'width: 60px;'
					),
					'type' => 'raw',
					'value' => '"<span id=\\"item-warehouse-inbound-history-$row\\">".
					($data->materialModelId?MaterialManager::getMaterialWarehouseInboundHistory($data->materialModelId,
			' . $model->projectId . ',"' . $model->receiptDate . '"):"")."</span>"',
				),
				array(
					'header' => '上次入库',
					'headerHtmlOptions' => array(
						'style' => 'width: 88px;'
					),
					'type' => 'raw',
					'value' => '"<span id=\\"item-warehouse-inbound-last-$row\\">".
					($data->materialModelId?MaterialManager::getMaterialWarehouseInboundLast($data->materialModelId,
			' . $model->projectId . ',"' . $model->receiptDate . '"):"")."</span>"',
				),
				array(
					'header' => '已报销',
					'headerHtmlOptions' => array(
						'style' => 'width: 60px;'
					),
					'type' => 'raw',
					'value' => '"<span id=\\"item-receipt-history-$row\\">".
					($data->materialModelId?MaterialManager::getMaterialReceiptHistory($data->materialModelId,
			' . $model->projectId . ',"' . $model->receiptDate . '", "' . $model->id
						. '"):"")."</span>"',
				),
				array(
					'header' => '上次报销',
					'headerHtmlOptions' => array(
						'style' => 'width: 88px;'
					),
					'type' => 'raw',
					'value' => '"<span id=\\"item-receipt-last-$row\\">".
					($data->materialModelId?MaterialManager::getMaterialReceiptLast($data->materialModelId,
			' . $model->projectId . ',"' . $model->receiptDate . '", "' . $model->id
						. '"):"")."</span>"',
				),
				array(
					'class' => 'CButtonColumn',
					'template' => '{delete}',
					'afterDelete' => 'function(){window.location.reload();}',
					'deleteButtonUrl' => 'Yii::app()->controller->createUrl("deleteItem",array("id"=>$data->primaryKey))'
				),
			),
		));
?>
 
<?php echo CHtml::submitButton('添加/保存'); ?>
<?php echo CHtml::endForm(); ?>
</div><!-- form -->
<div class="span-4">物品总价:
	<?php echo Yii::app()->format->formatCurrency($model->itemTotal); ?></div>
<div class="span-3"><?php echo CHtml::encode($model->getAttributeLabel('processingCost')); ?>:
	<?php echo Yii::app()->format->formatCurrency($model->processingCost); ?></div>
<div class="span-3"><?php echo CHtml::encode($model->getAttributeLabel('transportationCost')); ?>:
	<?php echo Yii::app()->format->formatCurrency($model->transportationCost); ?></div>
<div class="span-3"><?php echo CHtml::encode($model->getAttributeLabel('otherCost')); ?>:
	<?php echo Yii::app()->format->formatCurrency($model->otherCost); ?></div>
<div class="span-3"><?php echo CHtml::encode($model->getAttributeLabel('discount')); ?>:
	<?php echo Yii::app()->format->formatCurrency($model->discount); ?></div>
<div class="span-4 last"><?php echo '总计'; ?>:
	<?php echo Yii::app()->format->formatCurrency($model->getTotalPrice()); ?></div>
<div class="clear"></div>
</div>

<div class="dynamic-add-item">
<div>从入库单添加物品</div>
<div class="form">
<?php echo CHtml::beginForm(array(
	'dynamicAddWarehouseInboundItems',
	'id' => $model->id
));
?>
<div>
<?php echo CHtml::label('输入入库单号：', 'dynamic-material-warehouse-inbound-token'); ?>
<?php echo CHtml::textField('id', '', array(
	'id' => 'dynamic-material-warehouse-inbound-token'
));
?>
<?php echo CHtml::button('搜索',
	array(
		'id' => 'dynamic-material-warehouse-inbound-search',
		'class' => 'button'
	));
?>
</div>
<div id="dynamic-material-warehouse-inbound-list"></div>
<?php echo CHtml::submitButton('添加选中的物品', array(
	'confirm' => '确定要添加选中的物品么？'
)); ?>
<?php echo CHtml::endForm(); ?>
</div><!-- form -->
<?php
$dynamicLoadItemsUrl = CJSON::encode($this->createUrl('materialWarehouseInbound/dynamicLoadItems'));
$js = <<<EOF
$('#dynamic-material-warehouse-inbound-search').click(function(){
	var token=$('#dynamic-material-warehouse-inbound-token').val();
	if(id){
		$('#dynamic-material-warehouse-inbound-list').load($dynamicLoadItemsUrl+'?token='+token);
	}
});
EOF
;
Yii::app()->clientScript->registerScript('dynamic-material-warehouse-inbound-search', $js);
?>
</div><!-- dynamic-add-item -->

</div>
